﻿using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System;

namespace MusicPlayer
{

    public class Player
    {
        public delegate void PlayerStopedHandler(object sender, StoppedEventArgs e);

        private WaveOutEvent _waveOut;
        private AudioFileReader _audioFileReader;
        private VolumeSampleProvider _volumeSampleProvider;
        private PlayerStopedHandler _playerStopedHandler;
        private float _volume;
        private bool _isCut;
        public Player() 
        { 
            _waveOut = new WaveOutEvent();
        }

        public void Init()
        {
            _waveOut.Volume = _volume;
        }

        /// <summary>
        /// 播放
        /// </summary>
        public void Play(string filename)
        {
            if (_waveOut.PlaybackState == NAudio.Wave.PlaybackState.Stopped)
            {
                _audioFileReader = new AudioFileReader(filename);
                _volumeSampleProvider = new VolumeSampleProvider(_audioFileReader.ToSampleProvider());
                _volumeSampleProvider.Volume = 2.0f;
                _audioFileReader.Volume = 2.0f;
                _waveOut.Init(_volumeSampleProvider);
                _waveOut.PlaybackStopped += _waveOut_PlaybackStopped;
            }
            _waveOut.Play();
        }

        public void SetDefaultVolume(float volume)
        {
            _volume = volume;
        }

        public void SetPlayerStopped(PlayerStopedHandler playerStopedHandler)
        {
            _playerStopedHandler += playerStopedHandler;
        }

        private void _waveOut_PlaybackStopped(object sender, StoppedEventArgs e)
        {
            if (!_isCut)
            {
                _playerStopedHandler(sender, e);
            }
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            if (_waveOut != null)
            {
                _waveOut.Pause();
            }
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="code">停止代码：1-切歌</param>
        public void Stop(int code)
        {
            if (_waveOut != null)
            {
                _isCut = code == 1;
                _waveOut.Stop();
            }
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose() 
        {
            if (_audioFileReader != null)
            {
                _audioFileReader.Dispose();
            }
            if (_waveOut != null)
            {
                _waveOut.Dispose();
            }
        }

        /// <summary>
        /// 设置音量
        /// </summary>
        /// <param name="volume">音量</param>
        public void SetVolume(float volume)
        {
            _waveOut.Volume = volume;
        }

        public float GetVolume()
        {
            return _waveOut.Volume;
        }

        /// <summary>
        /// 总时长
        /// </summary>
        /// <returns></returns>
        public TimeSpan TotalTime()
        {
            return _audioFileReader.TotalTime;
        }

        /// <summary>
        /// 获取当前播放时间
        /// </summary>
        /// <returns></returns>
        public TimeSpan CurrentTime()
        {
            return _audioFileReader.CurrentTime;
        }

        /// <summary>
        /// 设置当前播放位置
        /// </summary>
        /// <param name="currentTime"></param>
        public void SetCurrentTime(TimeSpan currentTime)
        {
            _audioFileReader.CurrentTime = currentTime;
        }

        /// <summary>
        /// 获取播放器状态
        /// </summary>
        /// <returns></returns>
        public PlaybackState PlaybackState()
        {
            return _waveOut.PlaybackState;
        }
        
    }
}
